﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using BEGDC.CC.BL.Common;
using BEGDC.CC.BL.Workflow;
using BEGDC.CC.Entity;
using BEGDC.Utility.Castle;

namespace BEGDC.CC.Web.DicManagement
{
    /// <summary>
    /// Summary description for ShowTypeForm.
    /// </summary>
    public class ShowTypeForm : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.Label lblTitle;
        protected System.Web.UI.WebControls.Label lbName;
        protected System.Web.UI.WebControls.TextBox txtName;
        protected System.Web.UI.WebControls.Button btnSave;
        protected System.Web.UI.WebControls.CheckBox chkIsSys;
        protected System.Web.UI.HtmlControls.HtmlGenericControl span2;
        protected System.Web.UI.WebControls.Button btnClose;

        private void Page_Load(object sender, System.EventArgs e)
        {
            // Put user code to initialize the page here
            if (!Page.IsPostBack)
            {
                InitializeGlobalization();
                string id = Request.QueryString["id"];
                BindDataType(Convert.ToInt32(id));
                this.chkIsSys.Enabled = false;
            }
        }

        private void BindDataType(int id)
        {
            IUtility utility = AppContainer.Instance[ServiceConst.UtilityService] as IUtility;
            BEGDC.CC.Entity.DicType dic = utility.GetDicType(id);

            if (dic != null)
            {
                this.txtName.Text = dic.DicTypeKey;
                this.chkIsSys.Checked = dic.IsSys;
                if (this.chkIsSys.Checked)
                {
                    this.txtName.Enabled = false;
                    this.btnSave.Visible = false;
                }
            }
        }

        private void InitializeGlobalization()
        {
            this.lblTitle.Text = "Modify Data Type (修改数据类型)";
            this.lbName.Text = "Type Name: <br/>数据类型";
            //this.chkIsSys.Text = "Is System";
        }
        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.btnSave.Click += new EventHandler(btnSave_Click);
            this.btnClose.Click += new EventHandler(btnClose_Click);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

        void btnSave_Click(object sender, EventArgs e)
        {
            IUtility utility = AppContainer.Instance[ServiceConst.UtilityService] as IUtility;
            BEGDC.CC.Entity.DicType dic = new BEGDC.CC.Entity.DicType();

            dic.Id = Convert.ToInt32(Request.QueryString["id"]);
            dic.DicTypeKey = this.txtName.Text.Trim();

            dic.IsSys = this.chkIsSys.Checked;

            if (utility.SaveDicType(dic))
            {
                Page.RegisterClientScriptBlock("", "<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM001 + "');returnValue='Type';window.close();</script>");
                CCIdentity id = this.User.Identity as CCIdentity;
                BEGDC.CC.Entity.OperationLog log = new BEGDC.CC.Entity.OperationLog();
                log.Module = "DataManagement";
                Employee emp = new Employee();
                emp.Id = id.UserInfo.Id;
                log.Employee = emp;
                log.Time = System.DateTime.Now;
                log.Description = "Update data type.";
                utility.SaveOperationLog(log);
            }
            else
                Page.RegisterClientScriptBlock("", "<script>alert('" + BEGDC.CC.Resource.SystemMessage.CM002 + "');</script>");

        }

        void btnClose_Click(object sender, EventArgs e)
        {
            Page.RegisterClientScriptBlock("", "<script>returnValue='';window.close();</script>");
        }
    }
}
